﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.SqlClient;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;


public partial class Employ2 : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
       // showEmployList();
        showInforPersonal();
    }
    protected void logout_Click(object sender, EventArgs e)
    {
        Session.Contents.Remove("accountName");
        Session.Contents.Remove("accountType");
        Response.Redirect("Default.aspx", false);
    }
    protected void Button_Click(object sender, EventArgs e)
    {
        showEmployList();
    }
    public void showEmployList()
    {
        ConnectData cnData = new ConnectData();
        SqlDataReader result = cnData.dataReader("select * from tblStaff");
        DataTable tb = new DataTable();
        tb.Columns.Add("STT", typeof(Int32));
        tb.Columns.Add("Mã nhân viên", typeof(Int32));
        tb.Columns.Add("Họ và tên", typeof(string));
        tb.Columns.Add("Ngày sinh", typeof(string));
        tb.Columns.Add("Giới tính", typeof(string));
        tb.Columns.Add("Địa chỉ", typeof(string));
        tb.Columns.Add("Số điện thoại", typeof(string));
        tb.Columns.Add("Chức vụ", typeof(string));
        tb.Columns.Add("Phòng làm việc", typeof(string));
        int i = 1;
        while (result.Read())
        {
            DataRow row = tb.NewRow();
            row[0] = i++;
            row[1] = result["StaffNumber"];
            row[2] = result["FirstName"].ToString() + " " + result["LastName"].ToString();
            row[3] = getDate((DateTime)result["DateOfBirth"]);
            row[4] = result["Sex"];
            row[5] = result["Address"];
            row[6] = result["Tel"];
            row[7] = result["Position"];
            row[8] = result["AllocatedToWard"];
            tb.Rows.Add(row);
        }
        cnData.closeConnect();
        grvEmploy.DataSource = tb;
        grvEmploy.DataBind();
        visibleContent(employList);
    }
    protected void selectedEmploy(object sender, EventArgs e)
    {
        //Accessing BoundField Column
        string name = grvEmploy.SelectedRow.Cells[3].Text;

        //Accessing TemplateField Column controls
        string country = grvEmploy.SelectedRow.Cells[1].Text;
        view.InnerHtml = "<b>Name:</b> " + name + " <b>Country:</b> " + country;
        viewEmploy(Int32.Parse(grvEmploy.SelectedRow.Cells[2].Text));
    }
    public void viewEmploy(int indexSelect)
    {
        ConnectData cnData = new ConnectData();
        SqlDataReader result = cnData.dataReader("select * from tblStaff");
        DataTable tb = new DataTable();
        tb.Columns.Add("", typeof(string));
        tb.Columns.Add("Thông tin", typeof(string));

        while (result.Read())
        {
            int num = Int32.Parse(result["StaffNumber"].ToString());

            if (num == indexSelect)
            {
                eSNumber.InnerHtml = result["StaffNumber"].ToString();
                eFName.InnerHtml = result["FirstName"].ToString();
                eLName.InnerHtml = result["LastName"].ToString();
                eAddress.InnerHtml = result["Address"].ToString();
                eSex.InnerHtml = result["Sex"].ToString();
                eTel.InnerHtml = result["Tel"].ToString();
                eBirthday.InnerHtml = getDate((DateTime)result["DateOfBirth"]);
                eNin.InnerHtml = result["NIN"].ToString();
                ePosition.InnerHtml = result["Position"].ToString();
                eATWard.InnerHtml = result["AllocatedToWard"].ToString();
                eCSalary.InnerHtml = result["CurrentSalary"].ToString();
                eHWeek.InnerHtml = result["HoursPerWeek"].ToString();
                eSScale.InnerHtml = result["SalaryScale"].ToString();
                ePOTeamporary.InnerHtml = result["PermanentOrTemp"].ToString();
                ePWOMothly.InnerHtml = result["PaidWeekOrMonth"].ToString();
                eType.InnerHtml = result["QType"].ToString();
                ePosition1.InnerHtml = result["WEPosition"].ToString();
                eDate.InnerHtml = getDate((DateTime)result["QDate"]);
                eInstitution.InnerHtml = result["QInstitution"].ToString();
                eSDate.InnerHtml = getDate((DateTime)result["WEStartDate"]);
                eFDate.InnerHtml = getDate((DateTime)result["WEFinishDate"]);
                eOrganization.InnerHtml = result["WEOrganization"].ToString();
                // inforEmploy.InnerHtml = 
                //  "<div><"Thông tin chi tiết của nhân viên " + result["FirstName"].ToString() + " " + result["LastName"].ToString();
                /*addRow("Mã nhân viên: ", result["StaffNumber"].ToString(), ref tb);
                addRow("Địa chỉ: ", result["Address"].ToString(), ref tb);
                addRow("Giới tính: ", result["Sex"].ToString(), ref tb);
                addRow("Ngày sinh: ", result["DateOfBirth"].ToString(), ref tb);
                addRow("Số điện thoại: ", result["Tel"].ToString(), ref tb);
                addRow("Số bảo hiểm: ", result["NIN"].ToString(), ref tb);
                addRow("Chức vụ: ", result["Position"].ToString(), ref tb);
                addRow("Phòng làm việc: ", result["AllocatedToWard"].ToString(), ref tb);
                addRow("Lương hiện tại: ", result["CurrentSalary"].ToString(), ref tb);
                addRow("Thang bảng lương: ", result["SalaryScale"].ToString(), ref tb);
                addRow("Số giờ làm việc trong tuần: ", result["HoursPerWeek"].ToString(), ref tb);
                addRow("Hình thức thanh toán tiền lương: ", result["PaidWeekOrMonth"].ToString(), ref tb);
                addRow("Loại hợp đồng: ", result["PermanentOrTemp"].ToString(), ref tb); */
                break;
            }
        }
        visibleContent(inforEmploy);
        cnData.closeConnect();
    }
    public string getDate(DateTime dt)
    {
        return dt.ToString("dd/MM/yyyy");
    }
    public void addRow(ref Table tb, string str)
    {
        tb.Rows.Add(getRow(str));
    }

    public TableCell getCell(string str)
    {
        TableCell tbCell = new TableCell();
        tbCell.Text = str;
        return tbCell;
    }
    public TableRow getRow(string str)
    {
        TableRow tbRow = new TableRow();
        tbRow.Cells.Add(getCell(str));
        return tbRow;
    }
    public void visibleContent(Control ctChild)
    {
        foreach (Control child in content.Controls)
        {
            if (child == ctChild)
                child.Visible = true;
            else child.Visible = false;
        }
    }
    protected void btInfor_Click(object sender, EventArgs e)
    {
        showInforPersonal();
    }
    public void showInforPersonal()
    {
        visibleContent(inforPersonal);
    }
}